package light.head.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AuditStatusDAO;
import light.head.dao.oa.ResignationDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.AuditStatus;
import light.head.model.Resignation;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
 * 
 */
@Controller
@RequestMapping(value = Url.C_RESIGNATIONMANAGER)
public class ResignationManagerController extends SupporterController{
	
	private ResignationDAO reDAO;
	private UserInfoDAO userDAO;
	private OrganDAO organDAO;
	private AuditStatusDAO auDAO;
	private MessageDAO msgDAO;
	private MessageTemplateDAO mtDAO;
	
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.RESIGNATIONMANAGER_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED, Parm.TIME1, Parm.TITLE, Parm.TIME2, Parm.CN);
		
		// 获取根机构配置的物品申请流程节点
		//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (reDAO.findByMap_1(organDAO.getSonCodeList(cu_organCode(request)), cu_roleCode(request), map, pb) == RESULT.SUCCESS) {
			// 讲对应的审核状态放到对应的
			Resignation re = null;
			for (Object o : pb.getResultList()) {
				re = (Resignation) o;
				if (re.getHead() == false){
					re.setAuditStatusList(auDAO.findByBusinessMcRc(Sys.B_FIVE, re.getCode(), cu_roleCode(request)));
				}else{
					re.setAuditStatusList(auDAO.findByBusinessMcRc(Sys.B_SIX, re.getCode(), cu_roleCode(request)));
				}
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.RESIGNATIONMANAGER_LIST);
	}
	
	
	// 单个申请单详情
		@RequestMapping(value = Url.DETAIL)
		public ModelAndView detail(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
			if (null != code) {
				Resignation a = reDAO.get(code);
				if (null != a) {
					if (a.getHead() == false){
						a.setAuditStatusList(auDAO.findByBusinessMcRc(Sys.B_FIVE, a.getCode(), cu_roleCode(request)));
					}else{
						a.setAuditStatusList(auDAO.findByBusinessMcRc(Sys.B_SIX, a.getCode(), cu_roleCode(request)));
					}
					request.setAttribute(Parm.RESIGNATION, a);
				}
			}
			return new ModelAndView(Url.RESIGNATIONMANAGER_DETAIL);
		}

		
		@RequestMapping(Url.UPDATEDESIGNATE_1)
		public void updateDesignate1(String code, HttpServletRequest request, HttpServletResponse response) throws Exception {
			RESULT result = RESULT.ERROR;
			Resignation apply = reDAO.get(Integer.parseInt(code));
			Byte business ;
			if(apply.getHead() == false){
				business = Sys.B_FIVE;
			}else{
				business = Sys.B_SIX;
			}
			// 查询当前申请信息是否已被当前用户审核
			List<AuditStatus> asList = auDAO.findByRcMcAuBusiness(cu_roleCode(request), apply.getCode(), cu_code(request), business);
			if (asList != null && asList.size() > 0 ) {
				print(response, Msg.EXISTS);
				return;
			}
			
			// 1.判断是否有审核权限 查询流程配置表
			List<AuditStatus> auList = auDAO.findByRcMcBusiness(cu_roleCode(request), Integer.parseInt(code), business);
			if (auList == null || auList.size() < 0) {
				print(response, Msg.ERROR);
				return;
			}
			
			// 判断是否有上一级审核
			//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			Integer ishave = auDAO.ishave(cu_roleCode(request), Integer.parseInt(code), business, organDAO.getSonCodeList(cu_organCode(request)));
			if (ishave !=null && ishave > 1) {
				// 2.判断上一级审核节点是否已经审核通过，查询流程配置表和审核状态表
				Byte beforeStatus = auDAO.checkBeforeByBusinessRc(business, cu_roleCode(request), organDAO.getSonCodeList(cu_organCode(request)), Integer.parseInt(code));
				// 3.有审核权限，且上一级审核节点已经审核通过，进行保存本次审核状态
				if (beforeStatus == null || beforeStatus != Sys.B_ONE) {
					print(response, Msg.PARAMETER_ERROR);
					return;
				}
			}
			
			if (null != code) {
				List<AuditStatus> asList1 = auDAO.findAuditStatusBusiness(Integer.parseInt(code), cu_roleCode(request), business);
				for (AuditStatus as : asList1) {
					as.setStatus(Sys.B_ONE);
					as.setAu(cu_code(request));
					as.setAt(new Date());
					result = auDAO.upd(as);
					// 查询是否有下一节点，有就修改下一节点审核状态为当前审核状态并发送消息通知下一审核人，没有就通知申请提交人
					List<AuditStatus> pcList = auDAO.findByOcPcMcBusiness(as.getPc() + 1, Integer.parseInt(code), business);
					if (null != pcList && pcList.size() > 0) {
						for(AuditStatus aud : pcList){
							aud.setBeforeStatus(Sys.B_ONE);
							result = auDAO.upd(aud);
							Integer rc1 = userDAO.findByOc(aud.getCu(), aud.getOc());
							if (null != rc1 && rc1 == aud.getRc()){
								aud.setStatus(Sys.B_ONE);
								aud.setAt(new Date());
								aud.setAu(cu_code(request));
								List<AuditStatus> pc1List = auDAO.findByOcPcMcBusiness(aud.getPc() + 1, Integer.parseInt(code), business);
								if (null != pcList && pcList.size() > 0) {
									for(AuditStatus ad : pc1List){
										ad.setBeforeStatus(Sys.B_ONE);
										result = auDAO.upd(ad);
									}
									//审核中
									apply.setPass(Sys.B_FOUR);
									reDAO.upd(apply);
									 //消息通知审核人
									String name =userDAO.findCode(aud.getAu());
									Integer rc = auDAO.findByMcOcPc(cu_organCode(request), aud.getMc(), aud.getPc() + 1);	// 获取该机构具有审核申请信息的角色编号
									if(null == rc ){
										print(response, Msg.ERROR);
										return;
									}
									List<UserInfo> userList = userDAO.findByRoleRc(cu_organCode(request), rc);	// 获取有审核角色的所有用户
									List<Integer> rucs = new ArrayList<Integer>();
									for (UserInfo u : userList) {
										rucs.add(u.getCode());
									}
									
									String content = mtDAO.applyFirstToTwo(apply.getCt(), apply.getCn(), cu_realName(request), aud.getAt());
									msgDAO.sendToUsers("【" + name + "】提交申请单待审核通知", content, cu_code(request), cu_organCode(request), rucs);
									print(response,Msg.SUCCESS);
								} else {
									//全部通过
									apply.setPass(Sys.B_THREE);
									reDAO.upd(apply);
									String content = mtDAO.throughToCu(apply.getCt(), as.getAt());
									msgDAO.sendToUsers("【" + cu_realName(request) + "】审核通知", content, cu_code(request), cu_organCode(request), apply.getCu());
									print(response,Msg.SUCCESS);
								}
							} else {
								//审核中
								apply.setPass(Sys.B_FOUR);
								reDAO.upd(apply);
								 //消息通知审核人
								Integer rc = auDAO.findByMcOcPc(cu_organCode(request), as.getMc(), as.getPc() + 1);	// 获取该机构具有审核申请信息的角色编号
								if(null == rc ){
									print(response, Msg.ERROR);
									return;
								}
								List<UserInfo> userList = userDAO.findByRoleRc(cu_organCode(request), rc);	// 获取有审核角色的所有用户
								List<Integer> rucs = new ArrayList<Integer>();
								for (UserInfo u : userList) {
									rucs.add(u.getCode());
								}
								String content = mtDAO.applyFirstToTwo(apply.getCt(), apply.getCn(), cu_realName(request), as.getAt());
								msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请单待审核通知", content, cu_code(request), cu_organCode(request), rucs);
								print(response,Msg.SUCCESS);
							}
							aud.setBeforeStatus(Sys.B_ONE);
							result = auDAO.upd(aud);
						}
					} else if (RESULT.SUCCESS == result) {
						//全部通过
						apply.setPass(Sys.B_THREE);
						reDAO.upd(apply);
						String content = mtDAO.throughToCu(apply.getCt(), as.getAt());
						msgDAO.sendToUsers("【" + cu_realName(request) + "】审核通知", content, cu_code(request), cu_organCode(request), apply.getCu());
						print(response,Msg.SUCCESS);
					}
				}
			}
		}
		
		@RequestMapping(Url.UPDATEDESIGNATE_2)
		public void updateDesignate2(String code, String remark,HttpServletRequest request, HttpServletResponse response) throws Exception {
			Resignation apply = reDAO.get(Integer.parseInt(code));
			Byte business ;
			if(apply.getHead() == false){
				business = Sys.B_FIVE;
			}else{
				business = Sys.B_SIX;
			}
			// 查询当前申请信息是否已被当前用户审核
			List<AuditStatus> asList =  auDAO.findByRcMcAuBusiness(cu_roleCode(request), Integer.parseInt(code), cu_code(request),business);
			if (asList == null || asList.size() > 0) {
				print(response, Msg.EXISTS);
				return;
			}
			// 1.判断是否有审核权限 查询流程配置表
			List<AuditStatus> auList = auDAO.findByRcMcBusiness(cu_roleCode(request), Integer.parseInt(code), business);
			if (auList == null || auList.size() < 0) {
				print(response, Msg.ERROR);
				return;
			}
			// 判断是否有上一级审核
			//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			Integer ishave = auDAO.ishave(cu_roleCode(request), Integer.parseInt(code), business, organDAO.getSonCodeList(cu_organCode(request)));
			if (ishave !=null && ishave > 1) {
				// 2.判断上一级审核节点是否已经审核通过，查询流程配置表和审核状态表
				Byte status = auDAO.checkBeforeByBusinessRc(business, cu_roleCode(request), organDAO.getSonCodeList(cu_organCode(request)), Integer.parseInt(code));
				// 3.有审核权限，且上一级审核节点已经审核通过，进行保存本次审核状态
				if (status == null || status != Sys.B_ONE) {
					print(response, Msg.PARAMETER_ERROR);
					return;
				}
			}
			
			if (null != code) {
				List<AuditStatus> asList1 = auDAO.findAuditStatusBusiness(Integer.parseInt(code), cu_roleCode(request),business);
				for (AuditStatus as : asList1) {
					as.setStatus(Sys.B_TWO);
					as.setAu(cu_code(request));
					as.setAt(new Date());
					as.setRemark(remark);
					if (RESULT.SUCCESS == auDAO.upd(as)) {
						//不通过
						apply.setPass(Sys.B_TWO);
						reDAO.upd(apply);
						String content = mtDAO.rejectTofirst(apply.getCt(), cu_realName(request),as.getAt(), as.getRemark());
						msgDAO.sendToUsers("【" + cu_realName(request) + "】驳回了申请单", content, cu_code(request), cu_organCode(request), apply.getCu());
						print(response,Msg.SUCCESS);
					}
				}
			}
		}
		
		
		
	@Autowired
	public void setReDAO(ResignationDAO reDAO) {
		this.reDAO = reDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setAuDAO(AuditStatusDAO auDAO) {
		this.auDAO = auDAO;
	}
	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}

}
